import asyncio


class AsyncExamples:
    """异步编程示例"""

    async def io_operation(self, name, delay):
        """模拟I/O操作"""
        print(f"{name}: 开始I/O操作，需要{delay}秒")
        await asyncio.sleep(delay)
        print(f"{name}: I/O操作完成")
        return f"{name}_result"

    async def cpu_operation(self, name, iterations):
        """模拟CPU密集型操作"""
        print(f"{name}: 开始CPU计算")
        result = 0
        for i in range(iterations):
            result += i
        print(f"{name}: CPU计算完成")
        return result

    async def concurrent_operations(self):
        """并发操作示例"""
        tasks = [self.io_operation(f"任务{i}", i) for i in range(1, 4)]
        tasks.extend(self.cpu_operation("CPU任务", i) for i in range(10))

        print("=== 并发I/O操作 ===")
        results = await asyncio.gather(*tasks)
        print(f"并发结果: {results}")


# 使用示例
examples = AsyncExamples()
asyncio.run(examples.concurrent_operations())
